package com.sense.kukoo.common.utils;

import android.app.Activity;
import android.os.CountDownTimer;

/**
 * ClassName:  CountDownerSmsUtil
 * Description: 倒计时工具类
 * Author:      zhangqiang
 * Date:        2018/3/13 10:45
 */
public class CountDownerUtil extends CountDownTimer {
    private Activity mActivity;

    private OnFinishListener mOnFinishListenrer;

    public CountDownerUtil(Activity activity, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        mActivity = activity;
    }

    @Override
    public void onFinish() {
        if (mOnFinishListenrer != null) {
            mOnFinishListenrer.onFinish();
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (mActivity == null || mActivity.isFinishing()) {
            cancel();
            onFinish();
            return;
        }
        if (mOnFinishListenrer != null) {
            mOnFinishListenrer.onTick(millisUntilFinished);
        }
    }


    public interface OnFinishListener {
        void onFinish();
        void onTick(long millisUntilFinished);
    }

    public void setOnFinishListenrer(OnFinishListener mOnFinishListenrer) {
        this.mOnFinishListenrer = mOnFinishListenrer;
    }

}
